package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author John
 * @date 2022/12/07 0007
 * @apiNote
 */
@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper configMapper;

    @Autowired
    private ApArticleContentMapper contentMapper;

    /**
     * 根据参数加载文章列表
     *
     * @param loadtype 1 表示加载更新 2 表示加载更多
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //1.对数据校验
        if (null == loadtype) {
            loadtype = 1;
        }
        if (null == dto.getSize() || dto.getSize() == 0) {
            dto.setSize(10);
        }
        dto.setSize(Math.min(dto.getSize(), 30));

        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("__all__");
        }
        if (null == dto.getMinBehotTime() || dto.getMaxBehotTime() == null) {
            dto.setMinBehotTime(new Date(0));
            dto.setMaxBehotTime(new Date());
        }
        //2.调用mapper查询数据
        List<ApArticle> list = apArticleMapper.load(loadtype, dto);

        return ResponseResult.okResult(list);
    }

    /**
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.校验参数
        if (null == dto || StringUtils.isEmpty(dto.getContent())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.判断文章id是否存在，如果存在则修改，如果不存在则新增
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        if (null == dto.getId()) {
            //新增
            save(apArticle);

            ApArticleConfig config = new ApArticleConfig();
            config.setArticleId(apArticle.getId());
            config.setIsDown(false);
            config.setIsComment(true);
            config.setIsDelete(false);
            config.setIsForward(true);

            configMapper.insert(config);

            ApArticleContent articleContent = new ApArticleContent();
            articleContent.setContent(dto.getContent());
            articleContent.setArticleId(apArticle.getId());

            contentMapper.insert(articleContent);

        } else {
            //修改
            updateById(apArticle);

            ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId,
                    dto.getId()));
            articleContent.setContent(dto.getContent());

            contentMapper.updateById(articleContent);
        }
        //3.返回文章的id
        return ResponseResult.okResult(apArticle.getId());

    }
}
